package com.imagegallery;


import java.util.ArrayList;





import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;

import android.os.Bundle;
import android.provider.MediaStore.Images;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;

public class MainActivity extends Activity{
   
	
	int pageSize = 8;
	int pageNum = 1;
	ArrayList<String> imgList = new ArrayList<String>();
	Animation  slide_left_in;
	Animation  slide_left_out;
	
	Animation  slide_right_in;
	Animation  slide_right_out;
	
	
	void imageNextPre (String type){
		if("NEXT".equals(type)){
			pageNum++;
			
			
		}else if("PRE".equals(type)){
			pageNum--;	
		}
		
		ImageButton sdCardImg =null;
		String imgSrc ="";
		int currentImgPosition = 0;
		for(int i=0; i < pageSize; i++) {
			
			if(i ==0){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR01C01);
			}else if(i ==1){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR01C02);
			} else if(i ==2){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR01C03);
			} else if(i ==3){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR01C04);
			} else if(i ==4){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR02C01);
			} else if(i ==5){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR02C02);
			} else if(i ==6){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR02C03);
			} else if(i ==7){
				sdCardImg =  (ImageButton)findViewById(R.id.imgR02C04);
			}
			
			currentImgPosition = i+(pageSize*(pageNum-1));
			if(imgList.size() > currentImgPosition){
				imgSrc = imgList.get(currentImgPosition);	
					
				//BitmapFactory.Options bitmpaOption = new BitmapFactory.Options();
				Bitmap bitmap = BitmapFactory.decodeFile(imgSrc, null);
				 
				
				sdCardImg.setImageBitmap(bitmap); 
				//setImageBitmap(bitmap);
				sdCardImg.setTag(new SDCardItem(imgSrc,bitmap));
			}else{
				sdCardImg.setImageBitmap(null);
				sdCardImg.setTag(null);
			}
		}
	}
	
	class TouchListener implements OnTouchListener{
		int preX = 0;
		
		@Override
		public boolean onTouch(View view, MotionEvent motionEvt){
			
			if(preX==0){
				preX = (int)motionEvt.getX();
			}else{
				int minus = preX - (int)motionEvt.getX();
				if(minus >20 ){
					if(imgList.size() > pageSize*pageNum){
						applyAnimation(slide_left_out,slide_left_in,"NEXT");
					}
					
					preX =0;
				}else if(minus <-20 ){
					if(pageNum >1 ){
						applyAnimation(slide_right_out,slide_right_in,"PRE");
					}
					preX =0;
				}
			}
			return true;
		}
	}
	
	private void applyAnimation(Animation _out,Animation _in, String _type){
		
		final Animation in = _in;
		final TableLayout table = (TableLayout)findViewById(R.id.tableImgList);
		final String type = _type;
		
		
		// 밖으로 슬라이딩하는 애니메이션이 완료될때 텍스트가 화면 밖에 머물도록 보장한다.
		_out.setFillAfter(true);
		_out.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				imageNextPre(type);
				table.startAnimation(in);
				
			}
		});
		
		table.startAnimation(_out);
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        slide_left_in = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
    	slide_left_out = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
    	
    	slide_right_in = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
    	slide_right_out = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
        
        
    	//mGestureDetector = new GestureDetector(this,new SimpleOnGestureListener());
    	
    	TableLayout table = (TableLayout)findViewById(R.id.tableImgList);
    	table.setOnTouchListener(new TouchListener());
		
		Cursor c = getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null);
		if(c !=null && c.getCount() > 0){
			c.moveToFirst();
			for(int i=0; i < c.getCount(); i++) {
				imgList.add(c.getString(1));
    			c.moveToNext();
    		}
			c.close();
		}
		
		imageNextPre(null);
		
		
	
		 
		 final ImageButton imgR01C01 =  (ImageButton)findViewById(R.id.imgR01C01);
		 imgR01C01.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		 });
		 final ImageButton imgR01C02 =  (ImageButton)findViewById(R.id.imgR01C02);
		 imgR01C02.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		});
		 final ImageButton imgR01C03 =  (ImageButton)findViewById(R.id.imgR01C03);
		 imgR01C03.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		});
		 final ImageButton imgR01C04 =  (ImageButton)findViewById(R.id.imgR01C04);
		 imgR01C04.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		});
		 final ImageButton imgR02C01 =  (ImageButton)findViewById(R.id.imgR02C01);
		 imgR02C01.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		});
		 final ImageButton imgR02C02 =  (ImageButton)findViewById(R.id.imgR02C02);
		 imgR02C02.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		});
		 final ImageButton imgR02C03 =  (ImageButton)findViewById(R.id.imgR02C03);
		 imgR02C03.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		});
		 final ImageButton imgR02C04 =  (ImageButton)findViewById(R.id.imgR02C04);
		 imgR02C04.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				setSelectedImg((ImageButton)v);
			}
		});
		
		 
		 final ImageButton imgSelected =  (ImageButton)findViewById(R.id.imgSelected);
		 imgSelected.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					SDCardItem item = (SDCardItem)((ImageButton)v).getTag();
					if(item != null){
						ImageView saveImg = (ImageView)findViewById(R.id.saveImg);
						saveImg.setImageBitmap(item.bitmap);
						saveImg.setTag(item);
						LinearLayout linearpic = (LinearLayout)findViewById(R.id.linearPicture);
						linearpic.setVisibility(View.INVISIBLE);
					}
					
				}
			});
		
		 
		
        final Button btnImgSearch =  (Button)findViewById(R.id.btnImgSearch);
        btnImgSearch.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				LinearLayout linearpic = (LinearLayout)findViewById(R.id.linearPicture);
				linearpic.setVisibility(View.VISIBLE);
				
			}
		});
        
        final Button btnGalleryCancel =  (Button)findViewById(R.id.btnGalleryCancel);
        btnGalleryCancel.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				LinearLayout linearpic = (LinearLayout)findViewById(R.id.linearPicture);
				linearpic.setVisibility(View.INVISIBLE);
				
			}
		});
    }
    
  
    private void setSelectedImg(ImageButton v ){
    	SDCardItem item = (SDCardItem)v.getTag();
    	
    	if(item != null){
			ImageButton imgSelected = (ImageButton)findViewById(R.id.imgSelected);
			imgSelected.setImageBitmap(item.bitmap);
			imgSelected.setTag(item);
    	}    
    }
    class SDCardItem{
    	String src = "";
        Bitmap bitmap = null;
        
        SDCardItem(String src ,Bitmap bitmap){
        	this.src= src;
        	this.bitmap= bitmap;
        }
    }
}

